<?php
namespace app\api\validate\v2;

use app\api\validate\Base;

/**
 * 行为验证器
 * @package app\menus\validate
 */
class Publish extends Base
{

    //定义验证规则
    protected $rule = [
        'area_id|区域'  => 'require|number',
        'money|租金'  => 'require|number',
        'house_type|户型'  => 'require|number',
        'contract_type|合同方式'  => 'require|number',
        'type|类型'  => 'require|number',
        'name|姓名'  => 'require|chsAlpha',
        'phone|手机号'  => 'require',
        'wx_num|微信'  => 'require|chsDash',
        'lng|经度'  => 'require|float',
        'photos|图片'  => 'require|IsVarchar',
        'lat|纬度'  => 'require|float',
        'tags|标签'  => 'require|IsVarchar',
        'address|地址'  => 'require|IsAddressVarchar',
        'desc|简介'  => 'require|IsVarchar',
        'attachment|简介'  => 'require',
    ];

    //定义验证提示
    protected $message = [
        'area_id.require' => '请选择地区',
        'area_id.number' => '地区格式错误',
        'wx_num.require' => '微信号不能为空',
        'wx_num.chsDash' => '微信号错误',
    ];


    //定义验证场景
    protected $scene = [
        //更新
        'sublease'  =>  ['area_id','money','house_type','contract_type','type','name','wx_num'],
        'advice'  =>  ['area_id','name','wx_num'],
        'sublease_lng'  =>  ['lng'],
        'sublease_lat'  =>  ['lat'],
        'sublease_tags'  =>  ['tags'],
        'sublease_address'  =>  ['address'],
        'sublease_desc'  =>  ['desc'],
        'sublease_photos'  =>  ['photos'],
        'sublease_attachment'  =>  ['attachment'],
    ];

    //过滤特殊字符
    public function IsVarchar($value,$rule='',$date='',$field=''){
        return true;
        $file_array=[
            ['key'=>'desc','name'=>'简介'],
            ['key'=>'tags','name'=>'标签'],
            ['key'=>'address','name'=>'地址'],
        ];
        $file_array=array_column($file_array,null,'key');
        $str='/SELECT|INSERT|UPDATE|DOCUMENT|EVAL|DELETE|SCRIPT|ALERT|UNION|INTO|LOAD_FILE|OUTFILE|select|insert|update|CR|document|LF|eval|delete|script|alert|\'|\/\*|\#|\--|\ --|\/|\*|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\(|\)|\/|\/\/|\.\.\/|\.\/|union|into|load_file|outfile/';
        if(!preg_match($str,$value)){
            return true;
        }else{
            return $file_array[$field]['name'].'格式错误';
        }
    }
    //过滤特殊字符
    public function IsAddressVarchar($value,$rule='',$date='',$field=''){
        $file_array=[
            ['key'=>'desc','name'=>'简介'],
            ['key'=>'tags','name'=>'标签'],
            ['key'=>'address','name'=>'地址'],
        ];
        $file_array=array_column($file_array,null,'key');
        $str='/SELECT|INSERT|UPDATE|DOCUMENT|EVAL|DELETE|SCRIPT|ALERT|UNION|INTO|LOAD_FILE|OUTFILE|select|insert|update|CR|document|LF|eval|delete|script|alert|\'|\/\*|\#|\--|\ --|\/|\*|\+|\=|\~|\*@|\*!|\$|\%|\^|\&|\/|\/\/|\.\.\/|\.\/|union|into|load_file|outfile/';
        if(!preg_match($str,$value)){
            return true;
        }else{
            return $file_array[$field]['name'].'格式错误';
        }
    }
}
